home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 12 / BBS in a box XII-1.iso / Files / Unstuffers / Other Compression / MacGzip 0.21-src-c.sit / macgzip_021-src / macos / think / thePrefs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-03-25  |  12.9 KB  |  473 lines  |  [TEXT/KAHL]

  1. /*            SpinCursors by
  2.  *            America Online: LISPer
  3.  *             Internet: tree@uvm.edu
  4. */
  5. #ifndef _FOLDERS_
  6. #include <Folders.h>
  7. #endif
  8. #include <Script.h>
  9.  
  10. #include "thePrefs.h"
  11. #include "MacErrors.h"
  12.  
  13.  
  14. /************************************************************************************/
  15.  
  16. #define kPrefsDialogID        130
  17. #define kSaveButtonItem        1
  18. #define kCancelButtonItem    2
  19.  
  20. #define kBeepWD                14
  21. #define kQuitWD                5
  22.  
  23. #define kForce                7
  24.  
  25. #define kTEXTsuffix            3
  26. #define kTEXTcreator        4
  27. #define kTEXTlevel            10
  28.  
  29. #define kUpArrow            11
  30. #define kDownArrow            12
  31.  
  32. /* prefs file constants */
  33. #define kPrefsNameStrID        128
  34. #define kPrefsCreatorType    '\?\?\?\?'
  35. #define kPrefsFileType        'pref'
  36. #define kPrefsResourceType    'Pref'
  37. #define kPrefsResourceID    128
  38.  
  39. #define kFinderMessageStrID    -16397 /* ID of STR for default finder message */
  40.                                    /* see Inside Mac VI, page 9-22         */
  41. #define kStrType            'STR '
  42.  
  43. #define CmdKeyMask            0x0100
  44.  
  45. /*
  46.  * From DialogBits,C.K. Haun
  47.  * Apple Developer Tech Support
  48.  */
  49.  
  50.  /* key equates */
  51. enum  {
  52.     kEnterKey        = 0x03, 
  53.     kTabKey            = 9, 
  54.     kReturnKey        = 0x0D, 
  55.     kBackSpace        = 8, 
  56.     kEscKey            = 0x1B, 
  57.     kLeftArrowKey    = 0x1C, 
  58.     kRightArrowKey, 
  59.     kUpArrowKey,
  60.     kDownArrowKey, 
  61.     kDeleteKey        = 0x7F
  62. };
  63.  
  64. pascal Boolean filterIt(DialogPtr inputDialog, EventRecord *myDialogEvent, short *theDialogItem);
  65.  
  66. pascal OSErr GetStdFilterProc(ModalFilterProcPtr *theProc) = 
  67. {
  68.     0x303C, 0x0203, 0xAA68
  69. };
  70.  
  71. /* Indicates to the dialog manager which item is default.  Will then alias the return key */
  72. /* to this item, and also bold border it for you (yaaaaa!) */
  73. pascal OSErr SetDialogDefaultItem(DialogPtr theDialog, short newItem)
  74. {
  75.     0x303C, 0x0304, 0xAA68
  76. };
  77.  
  78. /* Indicates which item should be aliased to escape or Command - . */
  79. pascal OSErr SetDialogCancelItem(DialogPtr theDialog, short newItem)
  80. {
  81.     0x303C, 0x0305, 0xAA68
  82. };
  83. /* End From DialogBits */
  84.  
  85. /* these constants may be stored in the fields of the prefs struct */
  86.  
  87. OSErr GetPrefsName (StringPtr prefName);
  88.  
  89. PrefsType currPrefs;
  90. Boolean prefsChangedFlag;
  91.  
  92. short OpenPrefsResFile(SignedByte prefsPerm, Boolean createFlag)
  93. /* open the preferences file with the given permission; if createFlag is set,
  94.    create a preferences file if necessary */
  95. {
  96.     OSErr retCode;
  97.     short prefsVRefNum;
  98.     long prefsDirID;
  99.     Str255 prefsNameStr;
  100.     FSSpec prefsFSSpec;
  101.     short prefsResRefNum = -1;
  102.     
  103.     /* get the name of the prefs file */
  104.     retCode = GetPrefsName(prefsNameStr);
  105.     if (retCode == noErr) {
  106.     
  107.         /* Use FindFolder to locate the Preferences folder.
  108.         
  109.            MPW 3.2 and some other compilers provide the
  110.            glue necessary to use FindFolder (and Gestalt) safely
  111.            under System 6 and earlier.  Under System 6, the dirID
  112.            returned for the Preferences folder request will be
  113.            the System folder's dirID */
  114.  
  115.         retCode = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder,
  116.             &prefsVRefNum, &prefsDirID);
  117.         if (retCode == noErr) {
  118.         
  119.             /* make a file spec for the prefs file */
  120.             
  121.             retCode = FSMakeFSSpec(prefsVRefNum, prefsDirID, prefsNameStr,
  122.                 &prefsFSSpec);
  123.                 
  124.             if (retCode == fnfErr && createFlag) {
  125.                 /* prefs file doesn't already exist, so create it */
  126.                 FSpCreateResFile(&prefsFSSpec, kPrefsCreatorType, kPrefsFileType,
  127.                     smSystemScript);
  128.                 retCode = ResError();
  129.             }
  130.             
  131.             /* open the prefs file */
  132.             if (retCode == noErr) {
  133.                 prefsResRefNum = FSpOpenResFile(&prefsFSSpec, prefsPerm);
  134.             }
  135.         }
  136.     }
  137.     return prefsResRefNum;
  138. }
  139.  
  140. OSErr GetPrefsName (StringPtr prefName)
  141. /* get the name of the preferences file from a resource in the application */
  142. {
  143.     StringHandle prefStrHandle;
  144.     OSErr retCode = noErr;
  145.     
  146.     prefStrHandle = GetString(kPrefsNameStrID);
  147.     if (prefStrHandle != nil) 
  148.         BlockMove(*prefStrHandle, prefName, (Size) (**prefStrHandle) + 1);
  149.     else retCode = resNotFound;
  150.     
  151.     return retCode;
  152. }
  153.  
  154. Boolean GetOrGeneratePrefs( PrefsType * thePrefsTypePtr, long wantedVers )
  155. /* fill in the prefs structure from the resource file or from scratch */
  156. {
  157.     short prefsResRefNum;
  158.     Handle tempHandle;
  159.     extern    Boolean        gSavedPos;
  160.     extern    Point        gSavedPoint;
  161.     Str255 defaultStr = "\pttxt";
  162.     
  163.     /* initialize prefs structure in case we can't get a valid set */
  164.     thePrefsTypePtr->prefsTypeVers = wantedVers;
  165.     thePrefsTypePtr->compress = false;
  166.     thePrefsTypePtr->ascii = false;
  167.     thePrefsTypePtr->level = 6;
  168.     thePrefsTypePtr->force = false;
  169.     thePrefsTypePtr->suffix[0]=0x00;
  170.     BlockMove(defaultStr, thePrefsTypePtr->textcreator, *defaultStr+1);
  171.     thePrefsTypePtr->QuitWhenDone = true;
  172.     thePrefsTypePtr->BeepWhenDone = false;
  173.     thePrefsTypePtr->SavedPos = false;
  174.     thePrefsTypePtr->SavedPoint.h = 0;
  175.     thePrefsTypePtr->SavedPoint.v = 0;
  176.     
  177.     
  178.     /* open (but don't create) the prefs file */
  179.     prefsResRefNum = OpenPrefsResFile(fsRdPerm, false);
  180.     if (prefsResRefNum != -1) {
  181.     
  182.         /* file opened successfully, get the prefs resource */
  183.         tempHandle = Get1Resource(kPrefsResourceType, kPrefsResourceID);
  184.         
  185.         /* if the resource is there and it's the right size and version, copy it */
  186.         /* (in C these can be combined with &&, but don't use AND in Pascal)     */
  187.         
  188.         if (tempHandle != nil)
  189.             if (GetHandleSize(tempHandle) == sizeof(PrefsType)) 
  190.                 if ((*(PrefsType *)*tempHandle).prefsTypeVers == wantedVers) {
  191.                 
  192.                     /* copy the prefs struct */
  193.                     *thePrefsTypePtr = *(PrefsType *)*tempHandle;
  194.                     
  195.                 }
  196.         /* release the pref resource and close the file */
  197.         CloseResFile(prefsResRefNum);
  198.  
  199.         gSavedPos = thePrefsTypePtr->SavedPos;
  200.         gSavedPoint.h = thePrefsTypePtr->SavedPoint.h;
  201.         gSavedPoint.v = thePrefsTypePtr->SavedPoint.v;
  202.  
  203.     return true;
  204.     
  205.     }
  206.     else
  207.         return false;
  208. }
  209. OSErr SavePrefs(PrefsType * thePrefsTypePtr )
  210. /* save the prefs structure in the prefs resource file */
  211. {
  212.     OSErr retCode;
  213.     short prefsResRefNum;
  214.     Handle prefHandle, finderMessageHandle;
  215.     
  216.     /* open (and, if necessary, create) the prefs file */
  217.     prefsResRefNum = OpenPrefsResFile(fsRdWrPerm, true);
  218.     if (prefsResRefNum != -1) {
  219.     
  220.         /* file opened successfully, get the prefs resource */
  221.         prefHandle = Get1Resource(kPrefsResourceType, kPrefsResourceID);
  222.         
  223.         if (prefHandle == nil) {
  224.         
  225.             /* create a new resource */
  226.             prefHandle = NewHandle(sizeof(PrefsType));
  227.             if (prefHandle != nil) {
  228.             
  229.                 /* copy the prefs struct into the handle
  230.                    and make it into a resource */
  231.                 
  232.                 *(PrefsType *)*prefHandle = *thePrefsTypePtr;
  233.                 AddResource(prefHandle, kPrefsResourceType, kPrefsResourceID, 
  234.                     "\pPrefsType");
  235.                 retCode = ResError();
  236.                 if (retCode != noErr) DisposHandle(prefHandle);
  237.             } 
  238.             
  239.             else retCode = MemError(); /* NewHandle failed */
  240.         }
  241.         
  242.         else {  /* prefHandle != nil */
  243.         
  244.             /* update the existing resource */
  245.             SetHandleSize(prefHandle, sizeof(PrefsType));
  246.             retCode = MemError();
  247.             if (retCode == noErr) {
  248.  
  249.                 /* copy the prefs struct into the handle and tell the rsrc manager */
  250.                 *(PrefsType *)*prefHandle = *thePrefsTypePtr;
  251.                 ChangedResource(prefHandle);
  252.             }
  253.         }
  254.         if (retCode == noErr) {
  255.         
  256.             /* add the message to be displayed if the user tries
  257.                to open the prefs file in the Finder (but don't add it
  258.                if it's already in the preferences file) */
  259.                
  260.             finderMessageHandle = (Handle) GetString(kFinderMessageStrID);
  261.             if (finderMessageHandle != nil &&
  262.                 HomeResFile((Handle) finderMessageHandle) != prefsResRefNum) {
  263.             
  264.                 /* copy the resource into the prefs file */
  265.                 DetachResource(finderMessageHandle);
  266.                 AddResource(finderMessageHandle, kStrType, kFinderMessageStrID,
  267.                     "\pFinder message");
  268.                     
  269.                 /* if AddResource failed, dispose of the handle */
  270.                 retCode = ResError();
  271.                 if (retCode != noErr) DisposHandle(finderMessageHandle);
  272.             }
  273.         }
  274.         
  275.         /* update and close the preference resource file, 
  276.            releasing its resources from memory */
  277.         CloseResFile(prefsResRefNum);
  278.     }
  279.     
  280.     else {
  281.         /* couldn't open the res file */
  282.         retCode = ResError();
  283.         if (retCode == noErr) retCode = resFNotFound;
  284.     }
  285.     
  286.     return retCode;
  287. }
  288. /***********************Dialog*************/
  289. Boolean DoPrefsDialog(PrefsType * thePrefsTypePtr)
  290. /* display the prefs dialog; returns true if change made to prefs record */
  291. {
  292.     GrafPtr savePort;
  293.     DialogPtr prefDialogPtr;
  294.     short item;
  295.     Boolean doneFlag = false;
  296.     OSErr retCode;
  297.  
  298.     /* keep the new preference settings only in local variables
  299.        in case the user cancels */
  300.        
  301.     PrefsType newPrefsType;
  302.  
  303.     short        itemType;
  304.     Handle        itemHandle;
  305.     Rect        itemRect;
  306.     Str255        sTemp;
  307.     
  308.     Boolean changedFlag;
  309.  
  310.  
  311.     /* the old settings are our starting point, so copy them */
  312.     newPrefsType = *thePrefsTypePtr;
  313.     
  314.     /* throw up the dialog */
  315.     prefDialogPtr = GetNewDialog(kPrefsDialogID, nil, (WindowPtr) -1);
  316.     if (prefDialogPtr == nil) ExitToShell();  /* hurtin’ bad */
  317.     
  318.     SetDialogDefaultItem( prefDialogPtr, kSaveButtonItem);
  319.     SetDialogCancelItem( prefDialogPtr, kCancelButtonItem);
  320.  
  321.     /* set up the dialog to the initial values */
  322.  
  323.     GetDItem(prefDialogPtr, kForce, &itemType, &itemHandle, &itemRect);
  324.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.force ? 1 : 0));
  325.  
  326.     GetDItem(prefDialogPtr, kBeepWD, &itemType, &itemHandle, &itemRect);
  327.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.BeepWhenDone ? 1 : 0));
  328.  
  329.     GetDItem(prefDialogPtr, kQuitWD, &itemType, &itemHandle, &itemRect);
  330.     SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.QuitWhenDone ? 1 : 0));
  331. /*    HiliteControl ((ControlHandle) itemHandle, 255 );*/
  332.  
  333.     GetDItem(prefDialogPtr, kTEXTsuffix, &itemType, &itemHandle, &itemRect);
  334.     SetIText(itemHandle,thePrefsTypePtr->suffix);
  335.  
  336.     GetDItem(prefDialogPtr, kTEXTcreator, &itemType, &itemHandle, &itemRect);
  337.     SetIText(itemHandle,thePrefsTypePtr->textcreator);
  338.     SelIText(prefDialogPtr, kTEXTsuffix, 0, 32767);
  339.     
  340.     GetDItem(prefDialogPtr,kTEXTlevel, &itemType, &itemHandle, &itemRect);
  341.     NumToString( (long)thePrefsTypePtr->level, sTemp );
  342.     SetIText(itemHandle,sTemp);
  343.  
  344.     do {
  345.         GetPort(&savePort);
  346.         SetPort(prefDialogPtr);
  347.         ModalDialog((ModalFilterProcPtr)filterIt, &item);
  348.         switch(item) {
  349.         
  350.             case kSaveButtonItem:
  351.                 /* user wants to save the current or new settings */
  352.                 
  353.                 /* copy the editText string into the new prefs record */
  354.                 GetDItem(prefDialogPtr, kTEXTcreator, 
  355.                     &itemType, &itemHandle, &itemRect);
  356.                 GetIText(itemHandle, newPrefsType.textcreator);
  357.                 
  358.                 GetDItem(prefDialogPtr, kTEXTsuffix, 
  359.                     &itemType, &itemHandle, &itemRect);
  360.                 GetIText(itemHandle, newPrefsType.suffix);
  361.                 
  362.                 if(newPrefsType.textcreator[0]!=0x04)
  363.                 {
  364.                     Calert( "TEXT creator not valid" );
  365.                 }
  366.                 else
  367.                 {
  368.                         /* replace the old record with the new record */
  369.                         *thePrefsTypePtr = newPrefsType;
  370.                         doneFlag = true;
  371.                 }
  372.                 break;
  373.                 
  374.             case kCancelButtonItem:
  375.                 /* abandon the new preferences structure */
  376.                 doneFlag = true;
  377.                 break;
  378.                 
  379.             case kForce:
  380.             
  381.                 newPrefsType.force = !newPrefsType.force;
  382.                 GetDItem(prefDialogPtr, kForce, &itemType, &itemHandle, &itemRect);
  383.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.force ? 1 : 0));
  384.                 break;
  385.                 
  386.             case kBeepWD:
  387.             
  388.                 newPrefsType.BeepWhenDone = !newPrefsType.BeepWhenDone;
  389.                 GetDItem(prefDialogPtr, kBeepWD, &itemType, &itemHandle, &itemRect);
  390.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.BeepWhenDone ? 1 : 0));
  391.                 break;
  392.                 
  393.             case kQuitWD:
  394.             
  395.                 newPrefsType.QuitWhenDone = !newPrefsType.QuitWhenDone;
  396.                 GetDItem(prefDialogPtr, kQuitWD, &itemType, &itemHandle, &itemRect);
  397.                 SetCtlValue((ControlHandle) itemHandle, ( newPrefsType.QuitWhenDone ? 1 : 0));
  398.                 break;
  399.                 
  400.             case kUpArrow:
  401.             
  402.                 if ( newPrefsType.level < 9 )
  403.                 {
  404.                     newPrefsType.level++;
  405.                     GetDItem(prefDialogPtr,kUpArrow, &itemType, &itemHandle, &itemRect);
  406.                     InvertRect(&itemRect);
  407.                     while (StillDown())
  408.                     ;
  409.                     InvertRect(&itemRect);
  410.                     GetDItem(prefDialogPtr,kTEXTlevel, &itemType, &itemHandle, &itemRect);
  411.                     NumToString( (long)newPrefsType.level, sTemp );
  412.                     SetIText(itemHandle,sTemp);
  413.                 }
  414.                 break;
  415.                 
  416.             case kDownArrow:
  417.             
  418.                 if ( newPrefsType.level > 1 )
  419.                 {
  420.                     newPrefsType.level--;
  421.                     GetDItem(prefDialogPtr,kDownArrow, &itemType, &itemHandle, &itemRect);
  422.                     InvertRect(&itemRect);
  423.                     while (StillDown())
  424.                     ;
  425.                     InvertRect(&itemRect);
  426.                     GetDItem(prefDialogPtr,kTEXTlevel, &itemType, &itemHandle, &itemRect);
  427.                     NumToString( (long)newPrefsType.level, sTemp );
  428.                     SetIText(itemHandle,sTemp);
  429.                 }
  430.                 break;
  431.                 
  432.         }
  433.     } while (!doneFlag);
  434.     
  435.     DisposDialog(prefDialogPtr);
  436.     
  437.     SetPort(savePort);
  438.  
  439.     /* if the user clicked Save, return true */
  440.     return (item == kSaveButtonItem);
  441. }
  442.  
  443. pascal Boolean filterIt(DialogPtr inputDialog, EventRecord *myDialogEvent, short *theDialogItem)
  444. {
  445.     ModalFilterProcPtr theModalProc;
  446.     Boolean returnVal = false;
  447.     char theKey;
  448.     
  449.     if (((myDialogEvent->what == keyDown) || (myDialogEvent->what == autoKey)) &&
  450.         (myDialogEvent->modifiers & CmdKeyMask))
  451.     {
  452.         theKey = myDialogEvent->message & charCodeMask;
  453.         switch (theKey)
  454.         {
  455.             case kUpArrowKey:
  456.                             *theDialogItem = kUpArrow;
  457.                             returnVal = true;
  458.                             break;
  459.             case kDownArrowKey:
  460.                             *theDialogItem = kDownArrow;
  461.                             returnVal = true;
  462.                             break;
  463.         }
  464.     }
  465.     
  466.     if (!returnVal)
  467.         if (GetStdFilterProc(&theModalProc) == noErr)
  468.             returnVal=theModalProc(inputDialog, myDialogEvent, theDialogItem);
  469.  
  470.     return(returnVal);
  471. }